<?php
namespace backend\modules\member\controllers;


use backend\component\BackendBaseController;
use backend\component\ResultFormat;
use backend\models\UserForm;
use backend\service\UserService;
use common\models\member\User;
use common\models\member\UserAuthor;

class UserController extends BackendBaseController
{
    public function actions()
    {
        return [
            "switch" => [
                "class" => "backend\actions\SwitchAction",
                "modelName" => User::className(),
                "field" => "status",
            ],
        ];
    }
    /**
     * 用户列表
     */
    public function actionList() {
        if (\Yii::$app->request->getIsAjax()) {
            return UserService::userListData(\Yii::$app->request->post());
        }
        $model = new User();
        return $this->render("list",["model"=>$model]);
    }


    public function actionEdit($id=0) {
        $model = User::modelById($id);
        return $this->render("edit",["model"=>$model]);
    }


    public function actionModify($id=0) {
        $post = \Yii::$app->request->post("form");
        if (!count($post)) {
            return ResultFormat::error("非法提交表单");
        }
        $model = new UserForm();
        $model->id = $id;
        if ($model->load($post,"") && $model->modifyUser()) {
            return ResultFormat::success("操作成功");
        }
        return ResultFormat::error($model->getErrorsToString());
    }

    /**
     * 权限禁用
     */
    public function actionAssign($id) {
        if (isset($_GET["power"])) {
            return UserService::modifyUserPower($id,$_GET['power'],$_GET['recommend']);
        }
        $model = User::modelById($id);
        return $this->render("assign",["model"=>$model]);
    }


}